package leertaak2;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Socket;
import java.util.ArrayList;

/**
 * Parser. It parses the XML file by reading the next line in the
 * document. If it encounters a certain value it will get the according
 * value;
 * @author marcovellinga
 *
 */
public class Parser implements Runnable {
       
		// Variables
		private Socket client;
        public ArrayList<Data> allData = new ArrayList<Data>();
        
        /**
         * Constructor
         * @param client
         */
        public Parser(Socket client){
                this.client = client;          
        }
        
       /**
        * Run the parser. It parses the data and put's it in the data class
        * These data object are put in an arraylist and when we have 
        * the data of 10 stations it get's written to the file
        * according to the station.
        */
        public void run(){       
                
                // Variables
                String line;
                Data data = null;
                
                try
                {
                		// create reader for the input
                        BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
                        // Read next line
                        line = reader.readLine();
                        
                        while(line != null){               
                               
                        		// Write data if we have more then 10 stations
                        		if(allData.size() == 10){
                        			ArrayList<Data> copy = allData;
                        			new Writer(copy).insertData();
                        			allData.clear();
                        			
                        		}
                                
                                if(!line.equals("<?xml version=\"1.0\"?>")){
                                        line = line.trim();                      
                                        
                                        // New measurement from a station
                                        // New data need to be made
                                        if(line.contains("MEASUREMENT")){
                                                if(line.equals("<MEASUREMENT>")){
                                                        data = new Data();
                                                } else {
                                                        allData.add(data);
                                                }
                                        }
                                        // STN
                                        if(line.contains("STN")){
                                                line = removeBrackets(line);
                                                
                                                data.setStn(line);          
                                        }
                                        // DATE
                                        if(line.contains("DATE")){
                                                line = removeBrackets(line);
                                                
                                                data.setDate(line);        
                                        }
                                        // TIME
                                        if(line.contains("TIME")){
                                                line = removeBrackets(line);
                                                
                                                data.setTime(line);
                                        }
                                        // TEMP
                                        if(line.contains("TEMP")){
                                                line = removeBrackets(line);
                                                
                                                data.setTemp(line);
                                        }
                                        // DEWP
                                        if(line.contains("DEWP")){
                                            line = removeBrackets(line);
                                            
                                            data.setDewp(line);
                                        }
                                        // STP
                                        if(line.contains("STP")){
                                            line = removeBrackets(line);
                                            
                                            data.setStp(line);
                                        }
                                        // SLP
                                        if(line.contains("SLP")){
                                            line = removeBrackets(line);
                                            
                                            data.setSlp(line);
                                        }
                                        
                                        // VISIB
                                        if(line.contains("VISIB")){
                                            line = removeBrackets(line);
                                                
                                            data.setVisib(line);         
                                        }
                                        
                                        // WDSP
                                        if(line.contains("WDSP")){
                                            line = removeBrackets(line);
                                            
                                            data.setWdsp(line);
                                        }
                                        
                                        // PRCP
                                        if(line.contains("PRCP")){
                                            line = removeBrackets(line);
                                            
                                            data.setPrcp(line);
                                        }
                                        
                                        // SNDP
                                        if(line.contains("SNDP")){
                                            line = removeBrackets(line);
                                            
                                            data.setSndp(line);
                                        }
                                        
                                        // FRSHTT
                                        if(line.contains("FRSHTT")){
                                            line = removeBrackets(line);
                                            
                                            data.setFrshtt(line);
                                        }
                                        
                                        // CLDC
                                        if(line.contains("CLDC")){
                                            line = removeBrackets(line);
                                            
                                            data.setCldc(line);
                                        }
                                        
                                        // WNDDIR
                                        if(line.contains("WNDDIR")){
                                            line = removeBrackets(line);
                                            
                                            data.setWnddir(line);
                                        }
                                        
                                    
                                }
                                line = reader.readLine();
                        }
                        
                }
                catch(Exception e) {
                        e.printStackTrace();
                } 
        }
        
        /**
         * Remove brackets from a line
         * @param line
         * @return
         */
        public String removeBrackets(String line){
                return line.replaceAll("\\<.*?>","");
        }
        
        /**
         * Return the arraylist with all the data
         * @return
         */
        public ArrayList<Data> getAllData(){
                return this.allData;
        }

}